package com.lv.dhart.ui.fragment.fghome.inner.jjholdcount;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;

import com.lv.dhart.R;
import com.lv.dhart.base.AbsTopNavigationTabBaseFragment;
import com.lv.dhart.beans.JJHoldeBeans;
import com.lv.dhart.callback.IJJHoldCountCallback;
import com.lv.dhart.http.rxeasy.utils.HttpLog;
import com.lv.dhart.presenter.JJHoldCountPresenter;
import com.lv.dhart.ui.activity.JJHoldStockListActivity;
import com.smart.holder.iinterface.IViewHolderHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by binny on 2018/6/8.
 */

public class JJHoldCountFragment extends AbsTopNavigationTabBaseFragment<JJHoldeBeans.DataDTO> implements IJJHoldCountCallback, AdapterView.OnItemClickListener {
    protected List<JJHoldeBeans.DataDTO> dataDTOS = new ArrayList<>();
    private final JJHoldCountPresenter asBaseInfoPresenter = new JJHoldCountPresenter();
    private int offset = 0;
    private int limit = 20;

    public JJHoldCountFragment() {
    }

    @Override
    protected void initView(View view) {
        super.initView(view);

    }

    @Override
    protected void afterInitView() {
        super.afterInitView();
        mRefreshLayout.setEnableRefresh(true);
        mRefreshLayout.setEnableLoadMore(true);
        l.setOnItemClickListener(this);
    }

    @Override
    protected void getData() {
        asBaseInfoPresenter.getJJHoldCountList(offset, limit, this);
    }

    @Override
    protected void onRefresh() {
        offset = 0;
        asBaseInfoPresenter.getJJHoldCountList(offset, limit, this);
    }

    @Override
    protected void onLoadMore() {
        asBaseInfoPresenter.getJJHoldCountList(offset, limit, this);
    }

    @Override
    protected IViewHolderHelper initViewHolderHelper() {
        return new JJHoldCountViewHolderHelper();
    }

    @Override
    protected int initItemLayout() {
        return R.layout.item_fragment_jj_hold_count;
    }

    @Override
    protected List initListBean() {
        return dataDTOS;
    }


    @Override
    public void onJJHoldCountSuccess(JJHoldeBeans jjHoldeBeans) {
        if (offset == 0) {
            dataDTOS.clear();
        }
        offset = jjHoldeBeans.getOffset() + limit;
        dataDTOS.addAll(jjHoldeBeans.getData());
        mCommonAdapter.notifyDataSetChanged();
        mRefreshLayout.finishRefresh(true);
        mRefreshLayout.finishLoadMore(true);
    }

    @Override
    public void onJJHoldCountFailure(Throwable throwable) {
        HttpLog.e(throwable.getMessage());
        mRefreshLayout.finishRefresh(false);
        mRefreshLayout.finishLoadMore(true);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent i = new Intent(requireContext(), JJHoldStockListActivity.class);
        i.putExtra("jj_name", dataDTOS.get(position).getHolderName());
        startActivity(i);
    }
}
